package com.dharma.mydiet.storage;

import java.util.ArrayList;
import java.util.List;

import com.dharma.mydiet.MainApplication;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DAORegisterFood {

	public final static String KEY_ID = "idFoodRegister";
	public final static String KEY_ID_FOOD = "idFoodValue";
	public final static String KEY_ID_USER = "idUser";
	public final static String KEY_DATE = "date";
	public final static String KEY_QTY = "quantity";
	public final static String DATABASE_TABLE = "registerFood";
	SQLiteDatabase myDataBase;

	private static final String[] cols = new String[] { KEY_ID, KEY_ID_FOOD, KEY_ID_USER, KEY_DATE, KEY_QTY };

	private static DAORegisterFood instance;

	public static DAORegisterFood getInstance (){
		if (instance==null){
			instance=new DAORegisterFood();
		}
		return instance;
	}
	
	public DAORegisterFood() {
		myDataBase = DBManager.getInstance().getMyDataBase();
	}

	public void insertFood(TransactionalRegisterFood registerFood) {
		ContentValues newValues = new ContentValues();
		//newValues.put(KEY_ID, registerFood.getIdRegisterFood());
		newValues.put(KEY_ID_FOOD, registerFood.getIdFoodValue());
		newValues.put(KEY_ID_USER, registerFood.getIdUser());
		newValues.put(KEY_DATE, registerFood.getDateFormattedString());
		newValues.put(KEY_QTY, registerFood.getQuantity());
		myDataBase.beginTransaction();
		myDataBase.insert(DATABASE_TABLE, null, newValues);
		myDataBase.setTransactionSuccessful();
		myDataBase.endTransaction();
	}

	public boolean removeRegisterFood(TransactionalRegisterFood registerFood) {

		boolean ret = false;
		int row = this.getRow(String.valueOf(registerFood.getIdRegisterFood()));
		if (row >= 0) {
			ret = removeRegisterFood(row);
		}
		return ret;
	}

	private int getRow(String registerFoodId) {
		int row = -1;
		try {
			Cursor result = null;
			result = myDataBase.query(true, DATABASE_TABLE, cols, KEY_ID + "='" + registerFoodId + "'", null, null,
					null, null, null);

			if (!((result.getCount() == 0) || !result.moveToFirst())) {
				if (result.moveToFirst()) {
					row = result.getInt(result.getColumnIndex(KEY_ID));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return row;
	}

	public boolean removeRegisterFood(long _rowIndex) {
		myDataBase.beginTransaction();
		boolean result = myDataBase.delete(DATABASE_TABLE, KEY_ID + "=" + _rowIndex, null) > 0;
		myDataBase.setTransactionSuccessful();
		myDataBase.endTransaction();
		return result;

	}

	public boolean updateRegisterFood(TransactionalRegisterFood registerFood) {
		int row = this.getRow(String.valueOf(registerFood.getIdUser()));
		return updateRegisterFood(row, registerFood);
	}

	public boolean updateRegisterFood(Integer _rowIndex, TransactionalRegisterFood registerFood) {
		ContentValues newValues = new ContentValues();
		newValues.put(KEY_ID, registerFood.getIdRegisterFood());
		newValues.put(KEY_ID_FOOD, registerFood.getIdFoodValue());
		newValues.put(KEY_ID_USER, registerFood.getIdUser());
		newValues.put(KEY_DATE, registerFood.getDateFormattedString());
		newValues.put(KEY_QTY, registerFood.getQuantity());
		return myDataBase.update(DATABASE_TABLE, newValues, KEY_ID + "=" + _rowIndex, null) > 0;
	}

	public TransactionalRegisterFood getRegisterFood(String foodId) {
		TransactionalRegisterFood food = null;
		Cursor result = myDataBase.query(true, DATABASE_TABLE, cols, KEY_ID + "= '" + foodId + "'", null, null, null,
				null, null);
		if (!((result.getCount() == 0) || !result.moveToFirst())) {
			if (result.moveToFirst()) {
				food = new TransactionalRegisterFood(result.getInt(result.getColumnIndex(KEY_ID)), result.getInt(result
						.getColumnIndex(KEY_ID_FOOD)), result.getInt(result.getColumnIndex(KEY_ID_USER)),
						result.getString(result.getColumnIndex(KEY_DATE)), result.getFloat(result
								.getColumnIndex(KEY_QTY)));
			}
		}
		return food;
	}

	public ArrayList<TransactionalRegisterFood> getUsers() {
		ArrayList<TransactionalRegisterFood> devices = new ArrayList<TransactionalRegisterFood>();
		Cursor result = myDataBase.query(DATABASE_TABLE, cols,  KEY_ID_USER + "= '" + MainApplication.getCurrentUser().getIdUser() + "'", null, null, null, KEY_ID);
		if (result.moveToFirst())
			do {
				devices.add(new TransactionalRegisterFood(result.getInt(result.getColumnIndex(KEY_ID)), result
						.getInt(result.getColumnIndex(KEY_ID_FOOD)), result.getInt(result.getColumnIndex(KEY_ID_USER)),
						result.getString(result.getColumnIndex(KEY_DATE)), result.getFloat(result
								.getColumnIndex(KEY_QTY))));
			} while (result.moveToNext());
		return devices;
	}

	public static void create(TransactionalRegisterFood register) {

	}

	public static TransactionalRegisterFood read(int idRegister) {
		return null;

	}

	public static void update(TransactionalRegisterFood register) {

	}

	public static void delete(int idRegister) {

	}
}
